4
4
.
.
1
1
.
.
1
1
B
B
u
u
t
t
t
t
o
o
n
n
I
I
n
n
f
f
o
o
[
[
R
R
]
]
Button View allows User to click on the button in order to perform an Action and it contains two parts
action parameter defines code that should be executed hen Button is pressed
body defines Button's appearance since it can contain other Views: Text, Stack
Related tutorial Bind View to Action.
Syntax
Button ("Button") { print("Button was pressed") }
Button (action: { print("Button was pressed") } ) { Text("Button") }
S
S
i
i
m
m
p
p
l
l
e
e
B
B
u
u
t
t
t
t
o
o
n
n
In this simplest form Button appearance is defined with the simple String.
After that you define code which should be executed when Button is pressed.
Code
Button("Button") {
print("Button was pressed")
}
B
B
u
u
t
t
t
t
o
o
n
n
w
w
i
i
t
t
h
h
V
V
i
i
e
e
w
w
This example shows how to define Button appearance with other Views (in this case a singe Text View).
Code
Button(action: {
print("Button was pressed")
}) {
Text("Button")
}
View
Debug Area
Button was pressed
S
S
t
t
y
y
l
l
e
e
d
d
B
B
u
u
t
t
t
t
o
o
n
n
Code
Button(action: {
print("Button action")
}) {
HStack {
Image(systemName: "bookmark.fill")
Text("Button label")
.padding(10.0)
.overlay(
RoundedRectangle(cornerRadius: 10.0)
.stroke(lineWidth: 2.0)
.shadow(color: .blue, radius: 10.0)
)
}
}
View